package kr.co.hk.womeninfo;

import kr.co.hk.R;
import kr.co.hk.coupon.Coupon;
import kr.co.hk.date.DatePlaceMain;
import kr.co.hk.depart.Shopping_Main;
import kr.co.hk.fac.FacMain;
import kr.co.hk.local.LocalMain;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class WomenInfoMain extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		Wifi();

		Button btnDate = (Button) findViewById(R.id.btnDate);
		Button btnLocal = (Button) findViewById(R.id.btnLocal);
		Button btnFac = (Button) findViewById(R.id.btnFac);
		Button btnCoupon = (Button) findViewById(R.id.btnCoupon);
		Button btnDepart = (Button) findViewById(R.id.btnDepart);

		btnDepart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WomenInfoMain.this, Shopping_Main.class);
				startActivity(intent);
			}
		});
		btnCoupon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WomenInfoMain.this, Coupon.class);
				startActivity(intent);
			}
		});

		btnDate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WomenInfoMain.this, DatePlaceMain.class);
				startActivity(intent);
			}
		});
		btnLocal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Check GPS Enable
				LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
				if (!locationManager
						.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
					createGpsDisabledAlert();
				} else {
					Intent intent = new Intent(WomenInfoMain.this, LocalMain.class);
					startActivity(intent);
				}
			}
		});

		btnFac.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Check GPS Enable
				LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
				if (!locationManager
						.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
					createGpsDisabledAlert();
				} else {
					Intent intent = new Intent(WomenInfoMain.this, FacMain.class);
					startActivity(intent);
				}
				
			}
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			call_dialog();
			return true;
		}
		return super.onKeyDown(keyCode, event);

	}

	private void call_dialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("종료 하시겠습니까?").setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								WomenInfoMain.this.finish();
							}
						}).setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}

	private void Wifi() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		boolean isWifiConn = ni.isConnected();
		NetworkInfo nii = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		boolean isMobileConn = nii.isConnected();

		if (!isMobileConn && !isWifiConn) {
			AlertShow("Wifi 혹은 3G망이 연결되지 않았거나 원활하지 않습니다.네트워크 확인후 다시 접속해 주세요!");
		}
		if (isMobileConn) {
			AlertShoMobile("3G망 접속시 데이터요금이 부과될수 있습니다.");
			
		}
	}

	private void AlertShow(String msg) {

		AlertDialog.Builder alert_internet_status = new AlertDialog.Builder(
				this);
		alert_internet_status.setTitle("Warning");
		alert_internet_status.setMessage(msg);
		alert_internet_status.setPositiveButton("close",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						finish();
					}
				});
		alert_internet_status.show();
	}
	
	private void AlertShoMobile(String msg) {

		AlertDialog.Builder alert_internet_status = new AlertDialog.Builder(
				this);
		alert_internet_status.setTitle("Warning");
		alert_internet_status.setMessage(msg);
		alert_internet_status.setPositiveButton("close",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		alert_internet_status.show();
	}

	// GPS Disabled Alert
	private void createGpsDisabledAlert() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder
				.setMessage(
						"GPS 설정이 필요한 서비스 입니다. 설정 하시겠습니까?")
				.setCancelable(false).setPositiveButton("GPS 설정",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								showGpsOptions();
							}
						}).setNegativeButton("취소",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}

	// show GPS Options
	private void showGpsOptions() {
		Intent gpsOptionsIntent = new Intent(
				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		startActivity(gpsOptionsIntent);
	}

}